'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
    async index() {
        const { ctx } = this;
        ctx.body = 'hi, egg';
    }
    //获取首页文章
    async getArticleList() {
        let sql = `SELECT article_id as id, 
            article.title as title, 
            article.introduce as introduce, 
            article.addTime as addTime, 
            article.view_count as view_count ,
            type.typeName as typeName 
            FROM article LEFT JOIN type ON article.type_id = type.type_id 
            ORDER BY addTime DESC`
        console.log(sql)
        const results = await this.app.mysql.query(sql)

        this.ctx.body = {
            code: '200',
            msg: '文章查询成功',
            result: true,
            data: results
        }
    }


    //获取指定文章信息
    async getArticleById() {
        //先配置路由的动态传值，然后再接收值
        let id = this.ctx.params.id
        const results = await this.app.mysql.query('update article set view_count = view_count+1 where article_id =' + id)
        let sql = `SELECT article_id as id, 
            article.title as title, 
            article.content as content, 
            article.introduce as introduce, 
            article.addTime as addTime, 
            article.view_count as view_count ,
            type.typeName as typeName,
            type.type_id as type_id
            FROM article LEFT JOIN type ON article.type_id = type.type_id 
            where article_id= ${id}`
        console.log(sql)
        const result = await this.app.mysql.query(sql)

        this.ctx.body = {
            code: '200',
            msg: '文章查询成功',
            result: true,
            data: result
        }
    }

    //根据类别Id获取文章列表
    async getListById() {
        //先配置路由的动态传值，然后再接收值
        let id = this.ctx.params.id
        let sql = `SELECT article_id as id, 
            article.title as title, 
            article.introduce as introduce, 
            article.addTime as addTime, 
            article.view_count as view_count ,
            type.typeName as typeName 
            FROM article LEFT JOIN type ON article.type_id = type.type_id 
            where type.type_id= ${id} 
			ORDER BY addTime DESC`
        const results = await this.app.mysql.query(sql)

        this.ctx.body = {
            code: '200',
            msg: '文章查询成功',
            result: true,
            data: results
        }
    }

    // 根据文章id获取留言信息
    async getMessage() {
        let id = this.ctx.query.id
        console.log(id)
        if (id !== '' && id != undefined) {
            var sql = `select message.* from message
        left join article on article.article_id = message.article_id
        where message.article_id = ${id}`
        } else {
            var sql = `select message.*,article.title from message left join article on article.article_id = message.article_id`
        }
        
        console.log(sql)
        const results = await this.app.mysql.query(sql)
        this.ctx.body = {
            code: '200',
            msg: '留言查询成功',
            result: true,
            data: results
        }
    }

    // 根据文章id插入留言
    async insterMess() {
        const { article_id, content, name, email, lytime } = this.ctx.request.body;
        console.log(this.ctx.request.body)
        let sql = `insert into message (article_id,content,name,email,lytime) values(${article_id},'${content}','${name}','${email}','${lytime}')`
        const results = await this.app.mysql.query(sql)
        const insertSuccess = results.affectedRows === 1;
        this.ctx.body = {
            code: '200',
            result: true,
            isSuccess: insertSuccess,
            msg: "插入成功"
        }
    }
   
}

module.exports = HomeController;
`   `